package com.sixmac.police.service.impl;

import java.util.Map;

import com.sixmac.core.constant.Constant;
import com.sixmac.core.shiro.ShiroKit;
import com.sixmac.core.toolbox.Paras;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.AESKit;
import com.sixmac.core.toolbox.kit.CookieKit;
import com.sixmac.module.AdminUser;
import com.sixmac.police.filter.FilterUser;
import com.sixmac.police.service.AdminUserService;
import org.springframework.stereotype.Service;
import com.sixmac.core.base.service.BaseService;
import com.sixmac.core.plugins.dao.Md;
import com.sixmac.core.toolbox.CMap;
import com.sixmac.core.toolbox.support.Convert;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * AdminUserServiceImpl
 * 2018-09-26 11:15:36
 * @author Blade
 */
@Service
public class AdminUserServiceImpl extends BaseService<AdminUser> implements AdminUserService {

	@Override
	public boolean updateStatus(String ids, Integer status) {
		CMap map = CMap.init().set("status", status).set("ids", Convert.toIntArray(ids));
		boolean temp = updateBy("status = #{status}", "id in (#{join(ids)})", map);
		return temp;
	}

	@Override
	@SuppressWarnings("unchecked")
	public Map<String, Object> findOne(Object id) {
		return Md.selectOne("adminUser.findOne", CMap.init().set("id", id), Map.class);
	}

	@Override
	public AjaxResult login(String number, String password, Boolean save, HttpServletRequest request, HttpServletResponse response) {
		AdminUser user = findFirstBy("number = #{number} and password = #{password} ", Paras.create().set("number", number).set("password", ShiroKit.md5(password, "")));

		if (null == user)
			return error(USER_OR_PASSWORD_ERROR_MSG);

		//创建session
		HttpSession session = request.getSession();
		//屏蔽用户信息
		FilterUser.filterUser(user);

		//往session中储存用户信息
		session.setAttribute(Constant.SESSION_ADMIN_USER, user);
		if (null != save && save) {
			CookieKit.doSetCookie(Constant.COOKIE_USER, AESKit.encrypt(number + "-" + password), 7 * 24 * 60 * 60, "/", null, false, response);
		}

		return success("登陆成功");
	}
}
